package com.ltkj.common.rabbitmq.server;

import com.ltkj.common.core.utils.uuid.IdUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: zhangJiang
 * @Version： 1.0.0
 */
@SuppressWarnings(value = { "unchecked", "rawtypes" })
@Service
public class RabbitmqService {
    private static final Logger log = LoggerFactory.getLogger(RabbitmqService.class);
    @Resource
    private RabbitTemplate rabbitTemplate;


    public void rabbitSend(String echangeName,String routeKey,String message){
        String uniqueId = IdUtils.fastUUID();
        MessageProperties properties = new MessageProperties();
        properties.setContentEncoding("utf-8");
        properties.setDeliveryMode(MessageDeliveryMode.PERSISTENT);
        Message messageFormat = new Message(message.getBytes(), properties);
        rabbitTemplate.convertAndSend(echangeName, routeKey, messageFormat,new CorrelationData(uniqueId));
    }
    /**
     * @param no           消息
     * @param messageId    唯一id
     * @param exchangeName 交换机
     * @param key          路由键
     * @param delayTime    延迟时间(毫秒)
     */
    public void publish(String echangeName,String routeKey,String taskInfo, Integer delayTime) {
        rabbitTemplate.convertAndSend(echangeName, routeKey,taskInfo, message -> {
            // 设置超时时间  毫秒
            message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
            message.getMessageProperties().setExpiration((delayTime)+"");
            return message;
        });
    }

}
